package com.gopay.common.user.manager.impl;

import com.gopay.common.constants.cust.CustSmsAcConstants;
import com.gopay.common.constants.cust.CustSmsAcLogConstants;
import com.gopay.common.constants.cust.UserInfoConstants;
import com.gopay.common.constants.proccode.ProcCodeEnum;
import com.gopay.common.constants.trans.UserType;
import com.gopay.common.domain.UserInfo;
import com.gopay.common.domain.cust.CustCorpInfo;
import com.gopay.common.domain.cust.CustSmsAc;
import com.gopay.common.domain.cust.CustSmsAcLog;
import com.gopay.common.exception.GopayException;
import com.gopay.common.user.dao.CustCorpInfoQueryDAO;
import com.gopay.common.user.dao.CustSmsAcDAO;
import com.gopay.common.user.dao.CustSmsAcLogDAO;
import com.gopay.common.user.dao.DCUserCardInfoDao;
import com.gopay.common.user.manager.CustCorpInfoQueryManager;
import com.gopay.common.user.manager.CustSmsAcManager;
import com.gopay.common.user.manager.UserInfoQueryManager;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @ClassName: CustSmsAcManagerImpl
 * @Description: 短信认证接口实现类
 * @author lx
 * @date 2016年8月16日 上午10:44:55
 */
@Service("custSmsAcManager")
public class CustSmsAcManagerImpl implements CustSmsAcManager {

    private static final Logger logger = LoggerFactory.getLogger(CustSmsAcManagerImpl.class);

    @Resource(name = "userInfoQueryManager")
    private UserInfoQueryManager userInfoQueryManager;
    @Resource(name = "custSmsAcDAO")
    private CustSmsAcDAO custSmsAcDAO;

	@Resource(name = "dCUserCardInfoDao")
	private DCUserCardInfoDao dCUserCardInfoDao;
	
	@Resource(name = "custCorpInfoQueryManager")
	private CustCorpInfoQueryManager custCorpInfoQueryManager;
	
	@Resource(name="custSmsAcLogDAO")
    private CustSmsAcLogDAO custSmsAcLogDAO;
	
	@Resource(name="custCorpInfoQueryDao")
    private CustCorpInfoQueryDAO custCorpInfoQueryDao;
    
    @Override
    public CustSmsAc getCustSmsAcByCustId(String custId) {
        // 根据custId 获取 角色 999 的 管理员 userId
        // 到 CustSmsAc对应的表中 获取 短息认证状态 ,任何涉及短信认证的位置 都需要提前判断该状态是否已经锁定
        UserInfo userInfo = userInfoQueryManager.getUserInfoByCustIdAndOprId(custId, UserInfoConstants.CORP_ADMIN_CODE);
        if (userInfo != null) {
            return custSmsAcDAO.getCustSmsAcByUserId(userInfo.getUserId());
        }
        logger.info("商户{}不存在管理员", custId);
        return null;
    }

    @Override
    public String getCustSmsLockStatByCustId(String custId) {
        CustSmsAc custSmsAc = getCustSmsAcByCustId(custId);
        // 如果custSmsAc为空证明 Cust_Sms_Ac表中无记录，说明对应custId 未开启短信认证功能
        if (custSmsAc != null) {
        	String smsAcLockStat = custSmsAc.getSmsAcLockStat();
        	logger.info("custId="+ custId + ",短信锁定状态=" + smsAcLockStat);
            return smsAcLockStat;
        }
        logger.info("商户{}未开通短信认证功能", custId);
        return "";
    }

    @Override
    public void modifyCustSmsAc(CustSmsAc custSmsAc) {
        custSmsAcDAO.update(custSmsAc);
    }

	@Override
	public String getSmsAcStat(String custId) {
		CustCorpInfo custCorpInfo = custCorpInfoQueryManager.getByCustId(custId);
		if(custCorpInfo != null){
			String smsAcStat = StringUtils.isBlank(custCorpInfo.getSmsAcStat()) ? CustSmsAcConstants.SMSAC_CLOSE : custCorpInfo.getSmsAcStat();
			logger.info("------短信认证状态="+smsAcStat);
			return smsAcStat;
		} else{
			return null;
		}
	}

	@Override
	@Transactional
	public void openSmsAc(String custId, String operId) throws GopayException {
		if(StringUtils.isBlank(operId)){
			throw new GopayException("操作员id不能为空");
		} else if( StringUtils.isBlank(custId)){
			throw new GopayException("客户id不能为空");
		}
		if(!StringUtils.equals(operId, UserInfoConstants.CORP_ADMIN_CODE)){
			throw new GopayException("只有企业管理员可以开启短信验证服务");
		}
		CustCorpInfo custCorpInfo = custCorpInfoQueryManager.getByCustId(custId);
		
		String managerUserId = "";
		if(custCorpInfo != null){
			custCorpInfo.setSmsAcStat(CustSmsAcConstants.SMSAC_OPEN);
			//custCorpInfoService.editCustCorpInfo(custCorpInfo);
			/**
			 * 首次开启，将企业下对应的所有user插入到短信认证关联表中，并将企业管理员对应的记录中smsAcLockStat状态置为正常
			 */
			List<UserInfo> userList = userInfoQueryManager.getByCustId(custId);
			for (UserInfo user : userList) {
				if (StringUtils.equals(user.getUserType(), UserType.CorpManager.value)) {
					managerUserId = user.getUserId();
				}
				CustSmsAc custSmsAcExist = custSmsAcDAO.getCustSmsAcByUserId(user.getUserId());
				if (custSmsAcExist == null) {
					CustSmsAc custSmsAc = new CustSmsAc();
					custSmsAc.setCustId(custId);
					custSmsAc.setUserId(user.getUserId());
					if (StringUtils.equals(user.getUserType(), UserType.CorpManager.value)) {
						custSmsAc.setSmsAcLockStat("0");
					}
					custSmsAc.setTodaySmsErrNum(0);
					custSmsAc.setCreateTs(new Date());
					custSmsAc.setLastUpdTs(new Date());

					custSmsAcDAO.save(custSmsAc);
				}
			}
			/**
			 * 插入日志
			 */
			CustSmsAcLog custSmsAcLog = new CustSmsAcLog();
			custSmsAcLog.setCreateTs(new Date());
			custSmsAcLog.setLastUpdTs(new Date());
			custSmsAcLog.setUserId(managerUserId);
			custSmsAcLog.setCustId(custId);
			custSmsAcLog.setOperId(operId);
			custSmsAcLog.setOperType(CustSmsAcLogConstants.OPER_TYPE_04);
			custSmsAcLog.setOperContent(CustSmsAcLogConstants.OPER_CONTENT_016);
			custSmsAcLog.setCreateTs(new Date());

			custSmsAcLogDAO.save(custSmsAcLog);
		} else{
			throw new GopayException(ProcCodeEnum.PROC_CODE_100E5001);
		}
		custCorpInfoQueryDao.update(custCorpInfo);
	}
	
	@Override
	@Transactional
	public void closeSmsAc(String custId, String operId) throws GopayException {
		
		if(StringUtils.isBlank(operId)){
			throw new GopayException("操作员id不能为空");
		} else if( StringUtils.isBlank(custId)){
			throw new GopayException("客户id不能为空");
		}
		CustCorpInfo custCorpInfo = custCorpInfoQueryManager.getByCustId(custId);
		
		String managerUserId = "";
		List<UserInfo> userList = userInfoQueryManager.getByCustId(custId);
		for(UserInfo user : userList){
			if(StringUtils.equals(user.getUserType(), UserType.CorpManager.value)){
				managerUserId = user.getUserId();
			}
		}
		if(custCorpInfo != null){
			custCorpInfo.setSmsAcStat(CustSmsAcConstants.SMSAC_CLOSE);
			custCorpInfo.setLastUpdTs(new Date());
			/**
			 * 插入日志
			 */
			CustSmsAcLog custSmsAcLog = new CustSmsAcLog();
			custSmsAcLog.setCreateTs(new Date());
			custSmsAcLog.setUserId(managerUserId);
			custSmsAcLog.setCustId(custId);
			custSmsAcLog.setOperId(operId);
			if(StringUtils.equals(operId, UserInfoConstants.CORP_ADMIN_CODE)){
				custSmsAcLog.setOperType(CustSmsAcLogConstants.OPER_TYPE_05);
				custSmsAcLog.setOperContent(CustSmsAcLogConstants.OPER_CONTENT_017);
			} else{
				custSmsAcLog.setOperType(CustSmsAcLogConstants.OPER_TYPE_06);
				custSmsAcLog.setOperContent(CustSmsAcLogConstants.OPER_CONTENT_015);
			}
			custSmsAcLog.setLastUpdTs(new Date());

			custSmsAcLogDAO.save(custSmsAcLog);
		} else{
			throw new GopayException(ProcCodeEnum.PROC_CODE_100E5001,"客户不存在或已注销");
		}
		custCorpInfoQueryDao.update(custCorpInfo);
	}

	@Override
	public boolean hasSmsAcPermission(String userId) {
		UserInfo userInfo = userInfoQueryManager.getUserInfoByUserId(userId);
		if(userInfo != null && StringUtils.equals(userInfo.getUserType(), UserType.CorpManager.value)){
			return true;
		} else{
			return false;
		}
	}

	@Override
	public void displaySmsAc(String custId)  throws GopayException{
		CustCorpInfo custCorpInfo = custCorpInfoQueryManager.getByCustId(custId);
		if(custCorpInfo != null){
			custCorpInfo.setSmsAcDisplay(CustSmsAcConstants.SMSAC_DISPLAY);
			custCorpInfoQueryDao.update(custCorpInfo);
		} else{
			throw new GopayException(ProcCodeEnum.PROC_CODE_100E5001,"客户不存在或已注销");
		}
	}

	@Override
	public void hideSmsAc(String custId)  throws GopayException{
		CustCorpInfo custCorpInfo = custCorpInfoQueryManager.getByCustId(custId);
		if(custCorpInfo != null){
			custCorpInfo.setSmsAcDisplay(CustSmsAcConstants.SMSAC_HIDE);
			custCorpInfoQueryDao.update(custCorpInfo);
		} else{
			throw new GopayException(ProcCodeEnum.PROC_CODE_100E5001,"客户不存在或已注销");
		}
	}

	@Override
	public boolean isDisplaySmsAc(String custId) {
		CustCorpInfo custCorpInfo = custCorpInfoQueryManager.getByCustId(custId);
		if(custCorpInfo != null && StringUtils.equals(custCorpInfo.getSmsAcDisplay(), CustSmsAcConstants.SMSAC_HIDE)){
			return false; 
		} else{
			return true;
		}
	}

	@Override
	public String getSmsAcDisplayStat(String custId) {
		CustCorpInfo custCorpInfo = custCorpInfoQueryManager.getByCustId(custId);
		if(custCorpInfo != null){
			return StringUtils.isBlank(custCorpInfo.getSmsAcDisplay()) ? CustSmsAcConstants.SMSAC_DISPLAY : custCorpInfo.getSmsAcDisplay();
		} else{
			return null;
		}
	}

    
}
